<?php
namespace Common\Model;
use Think\Model;

/*
 * 文章内容model
 * @auth suosuo
 */

 class NewsModel extends Model {
 	private $_db = '';
	
	public function __construct (){
		$this->_db = M('news');
		
	}
	
	public function insert ($data = array() ){
		if(!is_array($data) || !$data){
			return 0;
		}
		$data['create_time'] = time();
		$data['username'] = getLoginUsername ();
		return $this->_db->add($data);
	}
	
	public function getNews ($data,$page,$pageSize=10) {
		$conditions = $data;
		if(isset($data['title']) && $data['title']){
			$conditions['title'] = array ('like','%'.$data['title'].'%');
		}
		if(isset($data['catid']) && $data['catid']){
			$conditions['catid'] = intval($data['catid']);
		}
		$offset = ($page - 1) * $pageSize;
		$list = $this->_db->where($conditions)
		->order('listorder desc ,news_id desc')
		->limit($offset,$pageSize)
		->select();
		return $list;
	}
	
	
	public function getNewsByAjax ($data,$page,$pageSize=10) {
		$conditions = $data;
		if(isset($data['catid']) && $data['catid']){
			$conditions['catid'] = intval($data['catid']);
		}
		$offset = $page;
		$list = $this->_db->where($conditions)
		->order('listorder desc ,news_id desc')
		->limit($offset,$pageSize)
		->select();
		return $list;
	}
	
	
	public function getNewsCount ($data = array()){
		$conditions = $data;
		if(isset($data['title']) && $data['title']){
			$conditions['title'] = array ('like','%'.$data['title'].'%');
		}
		if(isset($data['catid']) && $data['catid']){
			$conditions['catid'] = intval($data['catid']);
		}
		return $this->_db->where($conditions)->count();
	}
	
	
	public function updataStatusById($id,$status){//更新文章状态
		if(!is_numeric($id) || !$id){
			throw_exception("ID不合法");
		}
		if(!is_numeric($status) || !$status){
			throw_exception("状态不合法");
		}
		$data['status']=$status;
		return $this->_db->where('news_id='.$id)->save($data);
	}
	
	
	public function updataNewsListorderById($id,$listorder){
		if(!$id || !is_numeric($id)){
			throw_exception("ID不合法");
		}
		$data=array(
			'listorder'=>intval($listorder),
		);
		return $this->_db->where('news_id='.$id)->save($data);
		
	}


	public function getNewsById($id){
		if(!is_numeric($id) || !$id){
			throw_exception("ID不合法");
		}
		$data = $this ->_db->where('news_id='.$id)->find();
		return $data;
	}
	
	public function updataById ($id,$data) {
		if( !$id || !is_numeric($id) ){
			throw_exception('ID不合法');
		}
		if( !$data || !is_array($data) ){
			throw_exception('更新的数据不合法');
		}
		return $this->_db->where('news_id='.$id)->save($data);
	}
	
	public function getNewsByNewsIdIn ($newsIds) {
		if(!is_array($newsIds)){
			throw_exception("参数不合法");
		}
		$data = array (
			'news_id' =>array('in',implode(',', $newsIds))
		);
		return $this->_db->where($data)->select();
	}
	
	public function select ($data,$pageSize=6) {//获取最新的6片文章
		$conditions = $data;
		if(isset($data['title']) && $data['title']){
			$conditions['title'] = array ('like','%'.$data['title'].'%');
		}
		if(isset($data['catid']) && $data['catid']){
			$conditions['catid'] = intval($data['catid']);
		}
		
		$list = $this->_db->where($conditions)
		->order('listorder desc ,news_id desc')
		->limit(0,$pageSize)
		->select();
		return $list;
	}
 }
